var deviceone = require("deviceone");
var app = deviceone.sm("do_App");
var nf = deviceone.sm("do_Notification");
var network = deviceone.sm("do_Network");
var userUtil = require("helper/user");
var allpage = require("helper/allpage");
var qqCacher = require("helper/qqCacher");
var global = deviceone.sm("do_Global");
/**
 * 打开新页面
 * @source 页面ui地址
 * @dataJson 传递给新页面的json参数
 * @statusBarState 页面过渡方式
 */
var open = function(source,dataJson,statusBarState,id){
	if(!source || source===""){
		nf.toast("跳转页面无效");
		return;
	}
	global.setMemory("isInMessagePage", "0");//打开任何一个页面，都认为已离开“消息”页面
	var nstatus = network.getStatus();
	if(!nstatus || nstatus==null || "none"===nstatus.toLowerCase()){
		nf.toast("当前没有网络");
		if(source!=="source://view/startup.ui"){
			return;
		}
	}
	var upgradeDataJsonStr = qqCacher.upgradeDataJsonStr();
	if(upgradeDataJsonStr&&upgradeDataJsonStr!==""){
		openUpgrade(upgradeDataJsonStr);
		return;
	}
	if(source in allpage.uis){
		var uiVar = allpage.uis[source];
		if(uiVar.login==true && (userUtil.loginToken()==="undefined"||userUtil.loginToken()==="")){
			nf.toast("请先登录再操作");
			openLogin();
			return;
		}
		if(uiVar.authreal==true && userUtil.hasIdentityAuth()!=="1"){
			nf.toast("请先身份认证再操作");
			openRealAuth();
			return;
		}
	}
    var optionJson = {source:source};
    if(dataJson && dataJson!=null){
    	optionJson.data = dataJson;
    }
    if(statusBarState && statusBarState!=null && statusBarState!==""){
    	optionJson.statusBarState = statusBarState;
    }
    if(id && id!==""){
    	optionJson.id = id;
    }
    optionJson.animationType = "slide_r2l";//'slide_r2l': 从右至左滑，'push_r2l': 从右至左推出
    app.openPage(optionJson);
};
/**
 * 打开更新页面
 */
var openUpgrade = function(upgradeDataJsonStr){
	var isOpen = qqCacher.upgradePageIsOpen();
	if("1"!==isOpen){
		qqCacher.upgradePageIsOpen("1");
		var optionJson = {source:"source://view/public/upgrade/main.ui"};
		optionJson.data = upgradeDataJsonStr;
		app.openPage(optionJson);
	}
};
/**
 * 打开登陆页面
 */
var openLogin = function(){
	userUtil.loginToken("");
	open("source://view/user/login.ui");
};
/**
 * 打开身份认证提交页面
 */
var openRealAuth = function(){
	open("source://view/user/auth/edit.ui");
};
module.exports.openRealAuth = openRealAuth;
/**
 * 打开他、她的家页面
 */
var openPersonal = function(userId){
	if(userUtil.loginToken()==""){
		nf.toast("您还没有登陆!");
		return;
	}
	//open("source://view/user/personal/personal.ui",{userId : userId});
	open("source://view/user/usercenter/main.ui",{userId : userId});
};
/**
 * 打开买家订单详情页面
 */
var openBuyerOrder = function(orderNo){
	if(userUtil.loginToken()==""){
		nf.toast("您还没有登陆!");
		return;
	}
	open("source://view/order/detail.ui",{orderNo:orderNo});
};
/**
 * 打开卖家订单详情页面
 */
var openSellerOrder = function(orderNo){
	if(userUtil.loginToken()==""){
		nf.toast("您还没有登陆!");
		return;
	}
	open("source://view/order/seller/detail.ui",{orderNo:orderNo});
};
var openHKSellerOrder = function(orderNo){
	if(userUtil.loginToken()==""){
		nf.toast("您还没有登陆!");
		return;
	}
	open("source://view/homemaking/order/detail.ui",orderNo);
};
module.exports.open = open;
module.exports.openLogin = openLogin;
module.exports.openPersonal = openPersonal;
module.exports.openUpgrade = openUpgrade;
module.exports.openBuyerOrder = openBuyerOrder;
module.exports.openSellerOrder = openSellerOrder;
module.exports.openHKSellerOrder = openHKSellerOrder;